<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Yuyanwang extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $num = 0;
        if ( $bean->getProductType() == 0 ) {
            $num = $bean->getFaceValue() * $bean->getStartCharging();
        } elseif ($bean->getProductType() == 2){
            $num = $bean->getRechargeNum() * $bean->getStartCharging() . '元';
        } elseif ($bean->getProductType() == 3){
            $num = $bean->getRechargeNum() * $bean->getStartCharging() . 'M';
        }
        $array = [
            'szAgentId'     => $bean->getSupplierNum(), //商户号
            'szOrderId'     => $bean->getOrderNo(), //订单号
            'szPhoneNum'    => $bean->getAccount(),
            'nMoney'        => (int)$num,
            'nSortType'     => $bean->getOperatorCode(),
            'nProductClass' => 1,
            'nProductType'  => 1,
            'szProductId'   => $bean->getGoodsId(), //商品编码
            'szTimeStamp'   => date('Y-m-d H:i:s'),
            'params'        => json_encode($bean->getIp()),
            'szNotifyUrl'   => $bean->getNotifyUrl(), //异步回调地址
        ];
        $array['szVerifyString'] = md5('szAgentId='.$array['szAgentId']. '&szOrderId='.$array['szOrderId'].
            '&szPhoneNum='.$array['szPhoneNum']. '&nMoney='.$array['nMoney']. '&nSortType='.$array['nSortType'].
            '&nProductClass='.$array['nProductClass']. '&nProductType='.$array['nProductType']. '&szTimeStamp='.$array['szTimeStamp'].
            '&szKey='. $bean->getSecretKey());

        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('nFlag', '');
        $order_sn = $request->input('szOrderId', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, [2, 3]))->setRechargeSuccess($status == 2);
    }
}
